package com.xmu.makerspace.controller;

import com.xmu.makerspace.domain.TempTeam;
import com.xmu.makerspace.service.ManageRegisterService;
import com.xmu.makerspace.service.PrintFileService;
import com.xmu.makerspace.service.TeamInService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NonNls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * Created by status200 on 2017/7/29.
 */
@Controller
public class RouterController {

    @NonNls
    private static Log log = LogFactory.getLog(RegisterApiController.class);

    private final ManageRegisterService manageRegisterService;

    private final TeamInService teamInService;

    private final PrintFileService printFileService;

    @Autowired
    public RouterController(ManageRegisterService manageRegisterService, TeamInService teamInService, PrintFileService printFileService) {
        this.manageRegisterService = manageRegisterService;
        this.teamInService = teamInService;
        this.printFileService = printFileService;
    }

    @GetMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/admin-backend/index")
    public String adminBackend() {
        return "admin-backend";
    }

    @GetMapping(value = "/team-management/index")
    public String teamManagement() {
        return "team-management";
    }

    @GetMapping("/admin-backend/login")
    public String adminLogin() {
        return "admin-login";
    }

    @GetMapping("/team-management/login")
    public String teamLogin() {
        return "team-login";
    }

    @GetMapping("/update-register/{teamId:[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}}")
    public String updateRegister(@PathVariable("teamId") String teamId,Model model) {
        log.info(String.format("temp team \"%s\" try to update register information",teamId));

        model.addAttribute("tempTeamId",teamId);
        return "update-register";
    }

    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @GetMapping("/confirmEnter/{teamId:[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}}")
    public String confirmEnter(@PathVariable("teamId") String teamId,Model model) {
        TempTeam tempTeam = manageRegisterService.findTempTeamByTeamId(teamId);
        model.addAttribute("teamName",tempTeam.getTeamName());

        return "confirmEnter";
    }

    @GetMapping("/application-not-open")
    public String registerNotOpen() {
        return "error/application-not-open";
    }

    @GetMapping("/set-team-account/{teamId:\\d+}")
    public String setTeamAccount(@PathVariable("teamId") int teamId,Model model) {
        model.addAttribute("teamId", teamId);

        return "set-team-account";
    }

    @GetMapping("/admin-backend/print-file/{teamId:\\d+}")
    public String printFile(@PathVariable("teamId") int teamId,Model model) {
        model.addAttribute("team", printFileService.getCurrentTeamInfo(teamId));

        return "print-file";
    }
}
